﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SX.Data;
using SX.Common;
using SX.Utils;
using SX.Utils.Log.log4net;
using SX.Utils.Log;

namespace SX.Core
{
    public partial class BaseService
    { 

        protected static void Logger(string function, Action tryHandle, Action<Exception> catchHandle = null, Action finallyHandle = null)
        {
            Utils.Log.log4net.ILog L = LogManager.GetLogger(StackTraceHelper.GetClassFullName());
            LogHelper.Logger(L, function, ErrorHandle.Throw, tryHandle, catchHandle, finallyHandle);
        }

        protected static void Logger(string function, ErrorHandle errorHandleType, Action tryHandle, Action<Exception> catchHandle = null, Action finallyHandle = null)
        {
            Utils.Log.log4net.ILog L = LogManager.GetLogger(StackTraceHelper.GetClassFullName());
            LogHelper.Logger(L, function, errorHandleType, tryHandle, catchHandle, finallyHandle);
        }


        public void Logger(string position, string target, string type, object message)
        {
            using (var context = Db.Context().UseSharedConnection(true))
            {
                var user = AuthHelper.GetUserData<dynamic>();
                context.Insert("sys_log")
                    .Column("UserCode", user.UserCode)
                    .Column("UserName", user.UserName)
                    .Column("Position", position)
                    .Column("Target", target)
                    .Column("Type", type)
                    .Column("Message", JsonHelper.ToJson(message))
                    .Column("Date", DateTime.Now)
                    .Execute();
            }
        }
    }
}
